package com.dtb.trade.dao.mysql;

import com.dtb.trade.entity.Block;
import com.dtb.trade.entity.PointDeal;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

/**
 * @Author:ChengJian
 * @Description:
 * @Date: Created in 下午5:24 2018/8/9
 */
public interface TradeDao {



    @Select({
            "select * from t_point_deal where DEAL_DATE >= #{start} and DEAL_DATE <= #{end}"
    })
    @Results({
            @Result(column = "DEAL_ID",property = "dealId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "BUY_USER_ID",property = "buyUserId",jdbcType = JdbcType.INTEGER),
            @Result(column = "BUY_ORDER_ID",property = "buyOrderId",jdbcType = JdbcType.BIGINT),
            @Result(column = "SELL_USER_ID",property = "sellUserId",jdbcType = JdbcType.INTEGER),
            @Result(column = "SELL_ORDER_ID",property = "sellOrderId",jdbcType = JdbcType.BIGINT),
            @Result(column = "POINT_ID",property = "pointId",jdbcType = JdbcType.INTEGER),
            @Result(column = "DEAL_DATE",property = "dealDate",jdbcType = JdbcType.DATE),
            @Result(column = "DEAL_NUM",property = "dealNum",jdbcType = JdbcType.INTEGER),
            @Result(column = "DEAL_UNIT_PRICE",property = "dealUnitPrice",jdbcType = JdbcType.DOUBLE),
            @Result(column = "DEAL_PRICE",property = "dealPrice",jdbcType = JdbcType.DOUBLE),
    })
    List<PointDeal> getAll(Map<String,Object> map);


    @Select({
            "select * from t_block"
    })
    @Results({
            @Result(column = "block_index",property = "index",jdbcType = JdbcType.INTEGER),
            @Result(column = "block_hash",property = "hash",jdbcType = JdbcType.VARCHAR),
            @Result(column = "block_stamp",property = "timestamp",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "pointDeals",property = "data",jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "block_nonce",property = "nonce",jdbcType = JdbcType.INTEGER),
            @Result(column = "previousHash",property = "previousHash",jdbcType = JdbcType.VARCHAR)
    })
    List<Block> isHave();

    @Select({
            "select * from t_block order by block_index desc limit 0,1"
    })
    @Results({
            @Result(column = "block_index",property = "index",jdbcType = JdbcType.INTEGER),
            @Result(column = "block_hash",property = "hash",jdbcType = JdbcType.VARCHAR),
            @Result(column = "block_stamp",property = "timestamp",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "pointDeals",property = "data",jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "block_nonce",property = "nonce",jdbcType = JdbcType.INTEGER),
            @Result(column = "previousHash",property = "previousHash",jdbcType = JdbcType.VARCHAR)
    })
    Block getBlockIndex();

    @Insert({
            "insert into t_block (block_hash,block_stamp,pointDeals,block_nonce,previousHash) values(#{hash},#{timestamp},#{data},#{nonce},#{previousHash})"
    })
    int insertBlock(Block block);

    @Update({
            "update t_block set pointDeals = #{data} where block_index = #{index}"
    })
    int updateBlock(Block block);


    @Select({
            "select * from t_block where block_index = #{index}"
    })
    @Results({
            @Result(column = "block_index",property = "index",jdbcType = JdbcType.INTEGER),
            @Result(column = "block_hash",property = "hash",jdbcType = JdbcType.VARCHAR),
            @Result(column = "block_stamp",property = "timestamp",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "pointDeals",property = "data",jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "block_nonce",property = "nonce",jdbcType = JdbcType.INTEGER),
            @Result(column = "previousHash",property = "previousHash",jdbcType = JdbcType.VARCHAR)
    })
    Block selectBlock(@Param("index")int index);
}
